;2008-05-20
;Av Benjamin Ingberg och Patrik Johansson
;
;Bombers styrfil, ärver från character. Denna fil hanterar hur bomber samt bombexplosioner reagerar med
;resterande delar av världen

(load "character.scm")
(load "bomb-animation.scm")

(define bomb%
  (class character%
    (init x y power)
    (super-new [x x][y y][w 48][h 48][sprites (list (cons 'default "./sprites/bomb.gif"))])

    (define pow power)
    
    (define/override (get-type) 'bomb)
    (define bombcounter 300)
    
    (define/override (update board)
      (set! bombcounter (- bombcounter 1))
      (if (< bombcounter 0) (send board kill this)))
    
    (define/override (die board)
      (blow-bomb board))
    
    (define/public (blow-bomb board)
      (let ((x (send this get-x))
            (y (send this get-y))
            (w (send this get-w))
            (h (send this get-h)))
        
        (define (loop i mod-x mod-y)
          (let ((break #f)
                (objects (send board get-world-objects)))
            (if (< i pow)
                (begin (for-each
                        (lambda (obj)
                          (if (and (not (equal? this obj))
                                   (send obj
                                         collides?
                                         (mod-x x i)
                                         (mod-y y i)
                                         w
                                         h))
                              (let ((type (send obj get-type)))
                                (cond ((or (eq? type 'bomb)
                                           (eq? type 'monster)
                                           (eq? type 'player))
                                       (send board kill obj))
                                      ((eq? type 'wall)
                                       (send board kill obj)
                                       (set! break #t))
                                      ((eq? type 'super-wall)
                                       (set! break #t)))))) 
                        objects)
                       (if (not break)
                           (loop (+ i 1) mod-x mod-y)
                           i))
                i)))
        
        (define (nop x i)
          x)
        
        (let ((left (loop 0 - nop))
              (right (loop 0 + nop))
              (top (loop 0 nop -))
              (bottom (loop 0 nop +)))
          (send board add-world-object
                (new explosion-animation%
                     [x x]
                     [y y]
                     [l left]
                     [r right]
                     [u top]
                     [d bottom]
                     [board board])))))))
          
